package keter.combofs.model;

import com.google.common.base.Strings;
import keter.combofs.util.IOUtil;

/**
 * Created by gulx@neusoft.com on 2019/11/11.
 */
public class ComboFile {
    /**
     *  空ID：适用于通过URI保存的文件，此ID不可用于文件读取！
     */
    public static Long EMPTY_ID = 1L;

    private FileMeta meta;

    /**
     * 文件存储相对路径
     */
    private String uri = null;

    private String md5 = null;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    private Long id = null;

    public String getMeta() {
        return meta!=null?meta.get():"";
    }

    public String getMeta(String key) {
        return meta.get(key);
    }

    public void setMeta(FileMeta meta) {
        this.meta = meta;
    }

    public void addMeta(String name, String value) {
        this.meta.add(name,value);
    }

    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }

    public String setMD5(String md5){
        return this.md5 = md5;
    }

    public String getMD5(){
        return Strings.isNullOrEmpty(md5)?IOUtil.getMD5(fileBytes):md5;
    }

    /**
     * 读取文件字节流
     * @return
     */
    public byte[] getBytes() {
        return fileBytes;
    }

    public void setFileBytes(byte[] fileBytes) {
        this.fileBytes = fileBytes;
    }

    private byte[] fileBytes;

    public static  ComboFile build(Long id) {
        return build(id,null,null,null);
    }

    public static  ComboFile build(String uri, FileMeta meta) {
        return build(ComboFile.EMPTY_ID,uri,meta,null,null);
    }

    public static  ComboFile build(String uri, FileMeta meta, byte[] fileBytes, String md5) {
        return build(ComboFile.EMPTY_ID,uri,meta,fileBytes,md5);
    }

    public static  ComboFile build(Long id, String uri, FileMeta meta, byte[] fileBytes) {
        return build(id,uri,meta,fileBytes,null);
    }

    public static  ComboFile build(Long id, String uri, FileMeta meta, byte[] fileBytes, String md5) {
        ComboFile comboFile = new ComboFile();
        comboFile.setId(id);
        comboFile.setUri(uri);
        comboFile.setMeta(meta==null?FileMeta.build():meta);
        comboFile.setMD5(md5==null?"":md5);
        comboFile.setFileBytes(fileBytes);
        return comboFile;
    }

}
